/**
 *****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-8-11 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-8-11		Created.
 ****************************************************************************
 **/
package com.me.client.plugins.TicTacToe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

/**
 * 游戏面板. <br/>
 * 下午4:17:55 2012-8-11
 * 
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class TTTWindow extends JFrame {
	private static final long serialVersionUID = 1L;
	private static final int ENEMY_AI = 1;
	private TicTacToe myPlugin = null;
	private JPanel jPanelPlayer;
	private JLabel jLabelPlayerSelfInfo;
	private JLabel jLabelPlayerEnemyInfo;
	private JButton jButtonRestart;
	private JPanel jPanelBottom;
	private JButton jButtonCancel;
	private JPanel jPanelStatus;
	private JLabel jLabelStatus;
	private JPanel jPanelCenter;
	private JButton jButton1;
	private JButton jButton2;
	private JButton jButton3;
	private JButton jButton4;
	private JButton jButton5;
	private JButton jButton6;
	private JButton jButton7;
	private JButton jButton8;
	private JButton jButton9;
	private TTTGameLogic myGameLogic = null;
	public String[] symbols = { "", "X", "O" };
	private boolean userBegins;
	private int userSymbol;
	private int enemyType = ENEMY_AI;
	private int aiQuality;
	private String enemyName;

	public TTTWindow(TicTacToe plugin) {
		// TODO Auto-generated constructor stub
		this.myPlugin = plugin;
		this.initComponents();
	}

	/**
	 * 初始化组件.
	 */
	private void initComponents() {
		// TODO Auto-generated method stub
		this.setTitle("Tic-Tac-Toe Plugin");
		this.setMinimumSize(new Dimension(250, 250));

		jPanelPlayer = new JPanel();
		jPanelPlayer.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		jPanelPlayer.setLayout(new GridLayout(2, 1));
		jLabelPlayerSelfInfo = new JLabel();
		jLabelPlayerSelfInfo.setText("Player1: 0");
		jPanelPlayer.add(jLabelPlayerSelfInfo);
		jLabelPlayerEnemyInfo = new JLabel();
		jLabelPlayerEnemyInfo.setText("Player2: 0");
		jPanelPlayer.add(jLabelPlayerEnemyInfo);
		this.getContentPane().add(jPanelPlayer, BorderLayout.EAST);
		jPanelBottom = new JPanel();
		jButtonRestart = new JButton();
		jButtonRestart.setText("Restart");
		jButtonRestart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				restartGame(e);
			}
		});
		jPanelBottom.add(jButtonRestart);
		jButtonCancel = new JButton();
		jButtonCancel.setText("Cancel");
		jButtonCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelGame();
			}
		});
		jPanelBottom.add(jButtonCancel);
		this.getContentPane().add(jPanelBottom, BorderLayout.SOUTH);
		jPanelStatus = new JPanel();
		jPanelStatus.setLayout(new BorderLayout());
		jLabelStatus = new JLabel();
		jLabelStatus.setText("Waitting ...");
		jLabelStatus.setIcon(new ImageIcon(this.getClass().getResource("ttticon.png")));
		jPanelStatus.add(jLabelStatus, BorderLayout.CENTER);
		this.getContentPane().add(jPanelStatus, BorderLayout.NORTH);
		jPanelCenter = new JPanel();
		jPanelCenter.setLayout(new GridLayout(3, 3));
		jButton1 = new JButton();
		jButton1.setFont(new Font("Dialog", 1, 14));
		jButton1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(1);
			}
		});
		jPanelCenter.add(jButton1);
		jButton2 = new JButton();
		jButton2.setFont(new Font("Dialog", 1, 14));
		jButton2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(2);
			}
		});
		jPanelCenter.add(jButton2);
		jButton3 = new JButton();
		jButton3.setFont(new Font("Dialog", 1, 14));
		jButton3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(3);
			}
		});
		jPanelCenter.add(jButton3);
		jButton4 = new JButton();
		jButton4.setFont(new Font("Dialog", 1, 14));
		jButton4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(4);
			}
		});
		jPanelCenter.add(jButton4);
		jButton5 = new JButton();
		jButton5.setFont(new Font("Dialog", 1, 14));
		jButton5.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(5);
			}
		});
		jPanelCenter.add(jButton5);
		jButton6 = new JButton();
		jButton6.setFont(new Font("Dialog", 1, 14));
		jButton6.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(6);
			}
		});
		jPanelCenter.add(jButton6);
		jButton7 = new JButton();
		jButton7.setFont(new Font("Dialog", 1, 14));
		jButton7.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(7);
			}
		});
		jPanelCenter.add(jButton7);
		jButton8 = new JButton();
		jButton8.setFont(new Font("Dialog", 1, 14));
		jButton8.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(8);
			}
		});
		jPanelCenter.add(jButton8);
		jButton9 = new JButton();
		jButton9.setFont(new Font("Dialog", 1, 14));
		jButton9.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clickButton(9);
			}
		});
		jPanelCenter.add(jButton9);
		this.getContentPane().add(jPanelCenter, BorderLayout.CENTER);
		this.pack();
	}

	public void startNewGame(boolean userBegins, int userSymbol, int enemyType, int aiQuality, String enemyName) {
		System.out.println("----> in TTTWindow.startNewGame()");
		try {
			for (int i = 1; i < 10; i++) {
				((JButton) this.getClass().getDeclaredField("jButton" + i).get(this)).setEnabled(true);
			}
			this.userBegins = userBegins;
			this.userSymbol = userSymbol;
			this.enemyType = enemyType;
			this.aiQuality = aiQuality;
			this.enemyName = enemyName;
			if (this.myGameLogic == null) {
				this.myGameLogic = new TTTGameLogic(this);
			}
			this.jLabelPlayerEnemyInfo.setText(this.myPlugin.myName + ": " + this.myPlugin.winCounterUser);
			this.jLabelPlayerSelfInfo.setText(this.myPlugin.enemyName + ": " + this.myPlugin.winCounterEnemy);
			this.myGameLogic.startGame(this.userBegins, this.userSymbol, this.enemyType, this.aiQuality);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void clickButton(int filed) {
		if (this.myGameLogic.userAction(filed)) {
			this.myPlugin.setTurn(filed);
		}
	}

	/**
	 * 设置按钮标志.
	 * 
	 * @param index
	 *            按钮索引.
	 * @param symbol
	 *            按钮标志.
	 */
	public void setSymbolOnButton(int index, int symbol) {
		try {
			((JButton) this.getClass().getDeclaredField("jButton" + index).get(this)).setText(this.symbols[symbol]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 设置游戏进行状态.
	 * 
	 * @param status
	 */
	public void setGameStatus(String status) {
		this.jLabelStatus.setText(status.replaceAll("LOCAL", this.myPlugin.myName).replaceAll("ENEMY",
				this.myPlugin.enemyName));
	}

	/**
	 * 游戏结束显示设置.
	 * 
	 * @param winner
	 */
	public void gameEnds(int winner) {
		if (winner == 0) {
			// User wins
			this.myPlugin.winCounterUser++;
			this.markWinCombi(this.myGameLogic.lastWinCombi);
		} else if (winner == 1) {
			// enemy wins
			this.myPlugin.winCounterEnemy++;
			this.markWinCombi(this.myGameLogic.lastWinCombi);
		}
		this.jLabelPlayerSelfInfo.setText(this.myPlugin.myName + ": " + this.myPlugin.winCounterUser);
		this.jLabelPlayerEnemyInfo.setText(this.myPlugin.enemyName + ": " + this.myPlugin.winCounterEnemy);
	}

	/**
	 * 标记最终获胜一组合.
	 * 
	 * @param lastWinCombi
	 */
	private void markWinCombi(String combi) {
		int num = 0;
		try {
			for (int i = 0; i < 3; i++) {
				num = Integer.parseInt(combi.substring(i, i + 1));
				((JButton) this.getClass().getDeclaredField("jButton" + num).get(this)).setEnabled(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 和AI重新开始游戏.
	 */
	private void restartGame() {
		try {
			for (int i = 1; i < 10; i++) {
				((JButton) this.getClass().getDeclaredField("jButton" + i).get(this)).setEnabled(true);
			}
			this.userBegins = !this.userBegins;
			this.myGameLogic.startGame(this.userBegins, this.userSymbol, this.enemyType, this.aiQuality);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Restart按钮的处理方法.
	 * @param e
	 */
	private void restartGame(ActionEvent e){
		if(this.enemyType == ENEMY_AI){
			this.restartGame();
		}else{
			this.myPlugin.startNewGame(true);
		}
	}
	
	/**
	 * 取消游戏.
	 */
	private void cancelGame(){
		this.jLabelStatus.setText("Cannelling game...");
		this.myPlugin.endCurrentGame();
	}

	/**
	 * 
	 * @param parseInt
	 * @return
	 */
	public boolean setExternalAction(int index) {
		return this.myGameLogic.externalAction(index);
	}

}
